package com.tpp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tpp.base.feign.ImageFeign;
import com.tpp.base.mapper.ItemMapper;
import com.tpp.base.vo.*;
import com.tpp.base.mapper.ItemTypeMapper;
import com.tpp.base.service.ItemTypeService;
import com.tpp.comm.pojo.TpItem;
import com.tpp.comm.pojo.TpItemType;
import org.redisson.api.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemTypeServiceImpl implements ItemTypeService {

    @Resource
    private ItemTypeMapper itemTypeMapper;

    @Resource
    private ItemMapper itemMapper;

    @Resource
    private ImageFeign imageFeign;

    @Resource
    private RedissonClient redissonClient;

    @Value("${spring.cache.redis.key-prefix}")
    private String prefix;

    @Override
    @Cacheable(value = "index",key = "'lineNav'")
    public List<LineNavVo> queryLineNav() {
        LambdaQueryWrapper<TpItemType> queryWrapper = new QueryWrapper<TpItemType>().lambda();
        queryWrapper.isNotNull(TpItemType::getKey);
        queryWrapper.orderByAsc(TpItemType::getKey);
        List<TpItemType> tpItemTypes = itemTypeMapper.selectList(queryWrapper);
        List<LineNavVo> LineNavVos = tpItemTypes.stream().map(e->{
            LineNavVo vo = new LineNavVo();
            BeanUtils.copyProperties(e,vo);
            return vo;
        }).toList();
        return LineNavVos;
    }

    public List<NavLevel01> queryNavList(){
        LambdaQueryWrapper<TpItemType> queryWrapper = new QueryWrapper<TpItemType>().lambda();
        queryWrapper.eq(TpItemType::getParent,0);
        List<TpItemType> list01 = itemTypeMapper.selectList(queryWrapper);
        List<NavLevel01> list = list01.stream().map(tpItemType -> {
            NavLevel01 navLevel01 = new NavLevel01();
            BeanUtils.copyProperties(tpItemType,navLevel01);
            LambdaQueryWrapper<TpItemType> queryWrapper2 = new QueryWrapper<TpItemType>().lambda();
            queryWrapper2.eq(TpItemType::getParent,navLevel01.getId());
            List<TpItemType> tpItemTypes2 = itemTypeMapper.selectList(queryWrapper2);
            List<NavLevel02> navLevel02s = tpItemTypes2.stream().map(e -> {
                NavLevel02 navLevel02 = new NavLevel02();
                BeanUtils.copyProperties(e,navLevel02);
                return navLevel02;
            }).toList();
            navLevel01.setChildren(navLevel02s);

            LambdaQueryWrapper<TpItem> itemWrapper = Wrappers.lambdaQuery();
            itemWrapper.eq(TpItem::getItemType1Id,navLevel01.getId());
            itemWrapper.last(" limit 5");
            List<TpItem> tpItemTypes = itemMapper.selectList(itemWrapper);
            List<NavItem> navItems = tpItemTypes.stream().map(e -> {
                NavItem navItem = new NavItem();
                navItem.setId(e.getId());
                navItem.setItemName(e.getItemName());
                navItem.setMinPrice(e.getMinPrice());
                navItem.setImgUrl(imageFeign.findImage(tpItemType.getId(),1,2).getImgUrl());
                return navItem;
            }).toList();
            navLevel01.setHotItems(navItems);
            return navLevel01;
        }).toList();
        return list;
    }

    @Override
    public List<NavLevel01> queryNav() {
        RList<NavLevel01> list = redissonClient.getList(prefix+"index:nav");
        if (list==null||list.size()==0) {
            List<NavLevel01> navLevel01s = queryNavList();
            list.addAll(navLevel01s);
            return navLevel01s;
        }else{
            return list;
        }
    }

    @Override
    public List<ItemTypeVo> queryItemTypeByParent(Integer parent) {
        LambdaQueryWrapper<TpItemType> queryWrapper = new QueryWrapper<TpItemType>().lambda();
        queryWrapper.eq(TpItemType::getParent,parent);
        List<TpItemType> list01 = itemTypeMapper.selectList(queryWrapper);
        List<ItemTypeVo> collect = list01.stream().map(itemType -> {
            ItemTypeVo itemTypeVo = new ItemTypeVo();
            BeanUtils.copyProperties(itemType, itemTypeVo);
            return itemTypeVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
